import CustomError from "./CustomError"
module.exports = {
    /*
    * 云函数
    * */
    cloudFunction: function (name, data) {
        let {actionName, ...params} = data;
        return new Promise(function (resolve, reject) {
            wx.cloud.callFunction({
                name: name,
                data: {actionName, params},
                success: function (res) {
                    if (res.result.status === 0) {
                        resolve(res.result.data)
                    } else {
                        reject(res.result)
                    }
                },
                fail: err => {
                    reject(new CustomError(err, "Wx.callFunction"))
                }
            })
        })
    },

    /*图片压缩*/
    compressImage: function (filePath) {
        return new Promise(function (resolve, reject) {
            wx.compressImage({
                src: filePath, // 图片路径
                quality: 50, // 压缩质量
                success: function (res) {
                    // const tempFilePath = wx.getFileSystemManager().readFileSync(res.tempFilePath, "base64");
                    resolve(res.tempFilePath);
                }, fail: function (err) {
                    reject(new CustomError(err, "Wx.compressImage"))
                }
            })
        })
    },

    /*图片下载*/
    uploadFile: function (savePath, tempFilePath) {
        return new Promise(function (resolve, reject) {
            wx.cloud.uploadFile({
                cloudPath: savePath + new Date().getTime() + parseInt(10000 * Math.random()) + ".jpg",
                filePath: tempFilePath, // 文件路径
                success: function (res) {
                    resolve(res.fileID)
                },
                fail: err => {
                    reject(new CustomError(err, "Wx.uploadFile"))
                }
            })
        })
    },

    /*文件管理*/
    writeFile: function (buffer) {
        return new Promise(function (resolve, reject) {
            const wxFile = wx.getFileSystemManager();
            const filePath = wx.env.USER_DATA_PATH + "/wxCode.jpg";//把图片写在本地
            wxFile.writeFile({
                filePath,
                encoding: "binary",
                data: buffer,
                success: res => {
                    resolve({filePath: filePath})
                },
                fail: err => {
                    reject(new CustomError(err, "Wx.writeFile"))
                }
            })
        })
    },

    /*
    * 获取配置项
    * */
    getSetting: function (name) {
        if (!name) {throw new Error("getSetting授权名称不能为空")}
        return new Promise(function (resolve, reject) {
            wx.getSetting({
                success (res) {
                    if (res.authSetting[name] || !(name in res.authSetting)) {
                        resolve(true)
                    } else {
                        resolve(false) //用户已拒绝授权
                    }
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.getSetting"))
                }
            })
        })
    },

    /*
    * 打开授权设置
    * */
    openSetting: function (name) {
        return new Promise(function (resolve, reject) {
            wx.openSetting({
                success: function (res) {
                    if (res.authSetting[name]) {
                        resolve(true)
                    } else {
                        resolve(false) //返回 未同意授权
                    }
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.openSetting"))
                }
            })
        })
    },

    /*
    * 位置选择
    * */
    chooseLocation: function () {
        return new Promise(function (resolve, reject) {
            wx.chooseLocation({
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.chooseLocation"))
                }
            })
        })
    },

    /*
    * 活动定位信息
    * */
    getLocation: function () {
        return new Promise(function (resolve, reject) {
            wx.getLocation({
                type: "wgs84",
                isHighAccuracy: true,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    if (err.errMsg === "getLocation:fail system permission denied") { //用户未开启定位
                        wx.showModal({
                            title: "定位失败",
                            content: "请前往设置--打开手机定位服务",
                            showCancel: false
                        })
                    } else if (err.errMsg === "getLocation:fail auth deny") { //用户拒绝授权

                    } else {
                        reject(new CustomError(err, "Wx.getLocation"))
                    }
                }
            })
        })
    },

    /*
    * showModal 模态框
    * */
    showModal: function (content) {
        return new Promise(function (resolve, reject) {
            wx.showModal({
                content: content,
                success: function (modalRes) {
                    if (modalRes.confirm) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                }, fail: function (err) {
                    reject(new CustomError(err, "Wx.showModal"))
                }
            })
        })
    },

    /*
    * 消息订阅
    * */
    requestSubscribeMessage: function (list = []) {
        return new Promise(function (resolve, reject) {
            if (wx.requestSubscribeMessage) {
                wx.requestSubscribeMessage({
                    tmplIds: list,
                    fail: function (err) {
                        console.error(new CustomError(err, "Wx.requestSubscribeMessage").parse())
                    },
                    complete: function (res) {
                        resolve(res)
                    }
                })
            } else {
                resolve()
            }
        })
    },

}